﻿package net.wangit.view.element.component;

import net.wangit.LabelUtil;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class CheckBoxFieldElementWriter extends AbstractElementWriter {

	public CheckBoxFieldElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (!Checker.isEmpty(value))
			html.append(LabelUtil.get(ac, "components", (new StringBuilder(
					"element.52.")).append(value).toString()));
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		if (ec.getObject() != null) {
			write4Query(ac, html, ec, row);
			return;
		}
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (Checker.isEmpty(value))
			value = "0";
		html.append(
				"<span onclick=\"$f52(this)\"><input type=\"hidden\" name=\"")
				.append(ElementUtil.getAlias(ec)).append("\"");
		Html.setValue(html, value);
		html.append("/>");
		writeControl(ac, html, ec, row, value, false);
		html.append("</span>");
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		if (ec.getObject() != null) {
			write4Query(ac, html, ec, row);
			return;
		}
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (Checker.isEmpty(value))
			value = "0";
		writeControl(ac, html, ec, row, value, true);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		if (ec.getObject() != null) {
			write4Query(ac, html, ec, row);
			return;
		}
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (Checker.isEmpty(value))
			value = "0";
		writeControl(ac, html, ec, row, value, true);
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		ElementUtil.setHidden(ac, html, ec, row);
	}

	private void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, String value, boolean disabled) {
		html.append("<input type=\"checkbox\"");
		if (disabled)
			html.append(" disabled");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E7");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setChecked(html, value, "1");
		html.append("/>");
	}

	private void write4Query(ActionContext ac, StringBuffer html,
			ElementComponent element, Row row) {
		RadioElement radio = (RadioElement) element.getObject();
		radio.write(ac, html, 0, row);
	}
}
